/*
 * Project it.battlehorse.rcp.ioc
 * Created on Apr 9, 2006
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */

package it.battlehorse.ioc;

import java.util.HashSet;
import java.util.Set;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.EmptyVisitor;
import org.objectweb.asm.commons.Method;

/**
 * A method visitor which is able to identify object constructors.
 * 
 * @author battlehorse
 * @since Apr 9, 2006
 */
public class ConstructorVisitor extends EmptyVisitor {
	
	/* The set of identified constructors */
	private Set<Method> constructors;

	public ConstructorVisitor() {
		constructors = new HashSet<Method>();
	}
	
	/**
	 * Returns the set of constructors extracted from the last visit
	 * 
	 * @return the set of constructors extracted from the last visit
	 */
	public Set<Method> getConstructors() {
		return constructors;
	}
	
	
	@Override
	public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
	
		
		Type t = Type.getReturnType(desc);
		
		if (name.indexOf("<init>") != -1 && t.equals(Type.VOID_TYPE)) {
			constructors.add(new Method(name,desc));			
		}
		
		return super.visitMethod(access, name, desc, signature, exceptions);
	}
}
